package com.TCP_UDP网络编程.TCP_Socket_网络编程;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

@SuppressWarnings("all")
public class socketTCP03服务端_回复收到 {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端正在9990端口监听中~~~~");

        /// 1.监听
        /// 1.在本机的9990端口进行监听，等待连接
        ServerSocket serverSocket = new ServerSocket(9990);
        Socket socket = serverSocket.accept();
        System.out.println("class = "+socket.getClass() +"   IP"+ socket.getInetAddress());

        /// 2.读取客户端发来的图片内容
        String filePath = "java-code/src/com/网络编程/TCP_Socket_网络编程/TCP编程03_服务端接收到的图片.png";
        FileOutputStream fos = new FileOutputStream(filePath);   ///用来输出到指定路径的字节流；
        InputStream is = socket.getInputStream();               ///用来接受客户端发来的内容的字节输入流；
        int readLine = 0;                                       ///读取字节数
        byte buff[] = new byte[1024];                           ///存储内容的的数组
        /// 一边读取，一边写入到相应文件；
        while((readLine = is.read(buff)) != -1){
            fos.write(buff,0,readLine);
            fos.flush();
        }

        /// 3.回复给客户端；
        OutputStream outputStream = socket.getOutputStream();  ///得到输出流
        outputStream.write("收到图片".getBytes());              ///发送文字
        socket.shutdownOutput();                              ///发送完就通知客户端发送完毕；

        /// 4.关闭资源；
        fos.close();
        is.close();
        outputStream.close();
        socket.close();
        serverSocket.close();


    }
}
